<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <div th:replace="admin/head :: head"></div>

    <title>字典表单</title>
</head>
<body>
<div class="x-body layui-anim layui-anim-up">
    <form class="layui-form">
        <input type="hidden" name="id" th:value="${dictionary eq null ? '' : dictionary.id}"/>

        <div class="layui-form-item">
            <label for="L_name" class="layui-form-label">
                名称
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_name" name="name" required="" lay-verify="required" autocomplete="off"
                       class="layui-input" th:value="${dictionary eq null ? '' : dictionary.name}">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="L_value" class="layui-form-label">
                值
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_value" name="value" required="" lay-verify="required" autocomplete="off"
                       class="layui-input" th:value="${dictionary eq null ? '' : dictionary.value}">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="L_alias" class="layui-form-label">
                别名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_alias" name="alias" required="" lay-verify="required" autocomplete="off"
                       class="layui-input" th:value="${dictionary eq null ? '' : dictionary.alias}">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="L_type" class="layui-form-label">
                类型
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_type" name="type" required="" lay-verify="required" autocomplete="off"
                       class="layui-input" th:value="${dictionary eq null ? '' : dictionary.type}">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>

        <div class="layui-form-item">
            <button class="layui-btn" lay-filter="add" lay-submit="">
                保存
            </button>
        </div>
    </form>
</div>

<script>

    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;

        //自定义验证规则
        form.verify({});

        //监听提交
        form.on('submit(add)', function (data) {
            $.ajax({
                type: 'post',
                url: '/admin/dictionary/api/save',
                data: data.field,
                dataType: 'json',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader($("meta[name='_csrf_header']").attr("content"), $("meta[name='_csrf']").attr("content"));
                },
                success: function (result) {
                    if (result.status == 200) {
                        layer.alert(Messages.SUCCESS, {icon: 6}, function () {
                            // get frame index
                            var index = parent.layer.getFrameIndex(window.name);
                            // refresh parent list and close current dialog
                            parent.location.reload();
                            parent.layer.close(index);
                        });
                    } else {
                        layer.alert(result.error == null || result.error == undefined ? Messages.ERROR : result.error, {icon: 1});
                        return false;
                    }
                },
                error: function (e) {
                    layer.alert(Messages.ERROR, {icon: 1});
                }
            })
            return false;
        });
    });
</script>

</body>
</html>